package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.pojo.Employee;
import com.ithiema.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController{
    @Autowired
   IEmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        //   1.获取用户名+密码
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //   2、根据用户名查询用户信息
        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee queryEmp = employeeService.getOne(wrapper);

        //       判断:查询不到，返回登录失败
        if (queryEmp == null) {
            return R.error("用户名不存在");
        }
        //   3、如果查询到了，比对密码是否正确
        String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        //   判断: 比对失败，返回登录失败
        if (!pwd.equals(queryEmp.getPassword())) {
            return R.error("密码错误");
        }
        //   4、比对成功，说明用户名和密码是对的
        //   判断: 用户是否禁用
        if (queryEmp.getStatus() == 0) {
            return R.error("员工禁用");
        }
        //   5、用户状态正常。登录成功。
        //   5.1、存用户信息[只存用户的id] 到session。
        request.getSession().setAttribute("employee", queryEmp.getId());
        //   5.2、返回响应结果

        return R.success(queryEmp);

    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
//分页

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);
        Page pageInFo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        employeeService.page(pageInFo,wrapper);
        return R.success(pageInFo);
    }


    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){

        log.info("新增员工，员工信息：{}",employee.toString());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //通过session获取当前登录用户
        Long empId = (Long)request.getSession().getAttribute("employee");
        //设置创建时间和修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人和修改人信息   当前登录用户的id
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);
        return R.success("添加员工信息成功");
    }


    //查询
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
log.info("根据id查询");
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有查询到信息");

    }
    //更新
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        Long employeeId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());

        employeeService.updateById(employee);
        return R.success("更新成功");
    }

}
